package com.example.rlw.mvpplugintest.monitor;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.example.rlw.mvpplugintest.R;
import com.example.rlw.mvpplugintest.mvp.MVPBaseActivity;

import butterknife.BindView;

/**
 * 描述：tooBar 在 scrollView 滑动过程中  背景色的透明度变化
 * Created by rlw on 2019/3/19.
 */
public class MonitorActivity extends MVPBaseActivity implements MonitorScrollView.OnScrollChangedListener {
    @BindView(R.id.iv_pic)
    ImageView mIvPic;
    @BindView(R.id.ll_content)
    LinearLayout mLlContent;
    @BindView(R.id.sv_translu)
    MonitorScrollView mSvTranslu;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_monitor_layout;
    }

    @Override
    protected void initView() {
        mToolbar.setTitle("ToolBar");
        mToolbar.getBackground().setAlpha(0);  //先设置透明
        setSupportActionBar(mToolbar);
    }

    @Override
    protected void initData(@Nullable Bundle savedInstanceState) {

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            //设为 false
//           actionBar.setDisplayShowTitleEnabled(false);  //是否隐藏标题
            actionBar.setDisplayHomeAsUpEnabled(true);     //是否显示返回按钮
        }
        //实现透明状态栏效果  并且toolbar 需要设置  android:fitsSystemWindows="true"
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
            layoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | layoutParams.flags);
        }
        for (int i = 0; i < 20; i++) {
            TextView textView = new TextView(this);
            textView.setText("第" + i + "个");
            textView.setGravity(Gravity.CENTER);
            LinearLayoutCompat.LayoutParams params = new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dp2px(60));
            mLlContent.addView(textView, params);
        }
        mSvTranslu.setOnScrollChangedListener(this::onScrollChanged);
    }

    @Override
    protected void initNet() {

    }


    private int dp2px(int dp) {
        return (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, dp,
                getResources().getDisplayMetrics());
    }

    private int sp2px(int sp) {
        return (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP, sp,
                getResources().getDisplayMetrics());
    }

    @Override
    public void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt) {
        float height = mIvPic.getHeight();  //获取图片的高度
        if (oldt < height) {
            int i = Float.valueOf(oldt / height * 255).intValue();    //i 有可能小于 0
            mToolbar.getBackground().setAlpha(Math.max(i, 0));   // 0~255 透明度
        } else {
            mToolbar.getBackground().setAlpha(255);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //Toolbar上的左上角的返回箭头的键值为Android.R.id.home  不是R.id.home
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }
}
